package tests;

import junit.framework.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

import Model.AddSubTaskCommand;
import Model.ComplexTask;
import Model.Task;

/**
 * JUnit test case for AddSubTaskCommandTest
 */

public class AddSubTaskCommandTest extends TestCase {
	//declare reusable objects to be used across multiple tests
    AddSubTaskCommand t1;
	ArrayList<Task> arrList=null;
	Date utilDate = new Date();
	ComplexTask newtask;
	
	public AddSubTaskCommandTest(String name) {
		super(name);
	}

	@Before
	protected void setUp() {
	//define reusable objects to be used across multiple tests
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		    
		try { 

		 utilDate = df.parse("15/11/2011");
		 newtask = new ComplexTask("ParentTask",utilDate,"Complex");
	     arrList = new ArrayList<Task>();
	     arrList.add(newtask);
		 
		 t1 = new AddSubTaskCommand(arrList);
		 
				} 
				
				catch (ParseException e) {
					e.printStackTrace();
				}
			
	}

	@Test
	public void testAddSubTaskCommandConstructor() {

		AddSubTaskCommand var0 = new AddSubTaskCommand(arrList);
		
		assertNotNull("Object is created",var0);
		
	}
	
	@Test
	public void testExecute(){

		AddSubTaskCommand var0 = new AddSubTaskCommand(arrList);
		String var1 = "ParentTask";
		String var2 = "TaskName";
		String var3 = "Periodic";
		String var4 = "Daily";
		Date var5 = utilDate;
		
		try {
			var0.execute(var1, var2, var3, var4, var5);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		assertEquals(arrList.get(0).getSubtasks().size(),1);
		assertEquals(arrList.get(0).getSubtasks().get(0).getName(),var2);
		assertEquals(arrList.get(0).getSubtasks().get(0).getDueDate(),var5);
		assertEquals(arrList.get(0).getSubtasks().get(0).getType(),var3);

	}
	
	@Test
	public void testUndo() {
		AddSubTaskCommand var0 = new AddSubTaskCommand(arrList);
		String var1 = "ParentTask";
		String var2 = "TaskName";
		String var3 = "Periodic";
		String var4 = "Daily";
		Date var5 = utilDate;
		try {
			var0.execute(var1, var2, var3, var4, var5);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		assertEquals(arrList.get(0).getSubtasks().size(),1);
		var0.undo();
		assertEquals(arrList.get(0).getSubtasks().size(),0);
	
	}
	
	@Test
	public void testRedo() {
		
		AddSubTaskCommand var0 = new AddSubTaskCommand(arrList);
		String var1 = "ParentTask";
		String var2 = "TaskName";
		String var3 = "Periodic";
		String var4 = "Daily";
		Date var5 = utilDate;
		try {
			var0.execute(var1, var2, var3, var4, var5);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		var0.undo();
		var0.redo();
		assertEquals(arrList.get(0).getSubtasks().size(),1);
		assertEquals(arrList.get(0).getSubtasks().get(0).getName(),var2);
		assertEquals(arrList.get(0).getSubtasks().get(0).getDueDate(),var5);
		assertEquals(arrList.get(0).getSubtasks().get(0).getType(),var3);

	}
}